<?php

class CategoryController extends \BaseController {
    protected $layout = 'layouts.admin_panel';

    public function __construct (Category $category) {
        parent::__construct();
        $this->category = $category;
    }

	/**
     * Display a Category list
     * @author Thuan Truong
     * @return Response
     */
    public function index()
    {
        $categories = $this->category->getAllCategory();

        $view = View::make('category.index')->with(array(
            'categories' => $categories,
            'pageSize' => $this->pageSize
        ));
        if (Request::ajax()) {
            return $this->ajaxResponse($view, Lang::get('nav.page.user.index'));
        } else {
            $this->layout->content = $view;
        }
    }
	
	/**
     * View form create
     * @author Thuan Truong
     */
    public function create() {
        $view = View::make('category.create');
        if (Request::ajax()) {
            return $this->ajaxResponse($view, Lang::get('nav.page.user.create'));
        } else {
            $this->layout->content = $view;
        }
    }
	
	/**
     * Store category then click save button
     * @author Thuan Truong
     */
    public function store() {
        $input = array_map('trim', Input::all());   //trim input       
        $validator = $this->category->validate($input);

        if($validator->passes()){
                       
            $this->category->createCategory($input);
            Session::flash('f_notice', Lang::get('new.action.create.done'));            
            return Response::json(array(
                    'status' => 1,
                    'redirect' => route('admin.category.index'),
                    'message' => Lang::get('new.action.create.done')
            ));
            
        }else{
            if (Request::ajax()) {
            	$arr_error = $validator->messages()->getMessages();
                return Response::json(array(
                    'status' => 0, 
                    'code' => 'invalid_data', 
                    'messages' => $arr_error));
            }else{
                return Redirect::action('CategoryController@index');
            }
        }       
    }

	/**
     * view form edit
     * @author Thuan Truong
     */
    public function edit($id) {
    	$category = Category::find($id);
        $view = View::make('category.edit')->with(compact('category'));;
        if (Request::ajax()) {
            return $this->ajaxResponse($view, Lang::get('nav.page.user.create'));
        } else {
            $this->layout->content = $view;
        }
    }
	
	
	/**
     * edit category then click save button
     * @author Thuan Truong
     */
    public function update($id) {
        $input = array_map('trim', Input::all());   //trim input       
        $validator = $this->category->validate($input);

        if($validator->passes()){
                       
            $this->category->updateCategory($input, $id);
            Session::flash('f_notice', Lang::get('new.action.create.done'));            
            return Response::json(array(
                    'status' => 1,
                    'redirect' => route('admin.category.index'),
                    'message' => Lang::get('new.action.create.done')
            ));
            
        }else{
            if (Request::ajax()) {
            	$arr_error = $validator->messages()->getMessages();
                return Response::json(array(
                    'status' => 0, 
                    'code' => 'invalid_data', 
                    'messages' => $arr_error));
            }else{
                return Redirect::action('CategoryController@index');
            }
        }       
    }

	/**
     * update status category
     * @author Thuan Truong
     */
    public function updateStatus($id) {
        $category = Category::find($id);
        if ($category) {
            if ($category->status == 1) {
                $category->status = 0;
                $category->update();
            } else {
                $category->status = 1;
                $category->update();
            }
            
            return Response::json(array(
                'status' => 'SUCCESS',
            ));
            
        } else {
            return Response::json(array(
                'status' => 'ERROR',
            ));
        }
        
    }
	
	/**
	 * Remove the specified category from storage.
     * @author Trung Vuong
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id) {
        $category = $this->category->findCategoryById($id);
        $this->category->deleteCategory($id);
        
        if (Request::wantsJson()) {
            return Response::json(array(
                'status' => 1,
                'redirect' => route('admin.category.index'),
                'message' => 'Danh mục đã được xóa thành công'
            ));
        } else {
            return Redirect::action('CategoryController@index')->with('f_notice', 'Danh mục đã được xóa thành công');
        }
	}
}